home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / thread_cthread.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  3.8 KB  |  173 lines

  1. #include <mach/cthreads.h>
  2.  
  3.  
  4. /*
  5.  * Initialization.
  6.  */
  7. static void PyThread__init_thread _P0()
  8. {
  9.     cthread_init();
  10. }
  11.  
  12. /*
  13.  * Thread support.
  14.  */
  15. int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
  16. {
  17.     int success = 0;    /* init not needed when SOLARIS_THREADS and */
  18.                 /* C_THREADS implemented properly */
  19.  
  20.     dprintf(("PyThread_start_new_thread called\n"));
  21.     if (!initialized)
  22.         PyThread_init_thread();
  23.     /* looks like solaris detaches the thread to never rejoin
  24.      * so well do it here
  25.      */
  26.     cthread_detach(cthread_fork((cthread_fn_t) func, arg));
  27.     return success < 0 ? 0 : 1;
  28. }
  29.  
  30. long PyThread_get_thread_ident _P0()
  31. {
  32.     if (!initialized)
  33.         PyThread_init_thread();
  34.     return (long) cthread_self();
  35. }
  36.  
  37. static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
  38. {
  39.     dprintf(("PyThread_exit_thread called\n"));
  40.     if (!initialized)
  41.         if (no_cleanup)
  42.             _exit(0);
  43.         else
  44.             exit(0);
  45.     cthread_exit(0);
  46. }
  47.  
  48. void PyThread_exit_thread _P0()
  49. {
  50.     do_PyThread_exit_thread(0);
  51. }
  52.  
  53. void PyThread__exit_thread _P0()
  54. {
  55.     do_PyThread_exit_thread(1);
  56. }
  57.  
  58. #ifndef NO_EXIT_PROG
  59. static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
  60. {
  61.     dprintf(("PyThread_exit_prog(%d) called\n", status));
  62.     if (!initialized)
  63.         if (no_cleanup)
  64.             _exit(status);
  65.         else
  66.             exit(status);
  67.     if (no_cleanup)
  68.         _exit(status);
  69.     else
  70.         exit(status);
  71. }
  72.  
  73. void PyThread_exit_prog _P1(status, int status)
  74. {
  75.     do_PyThread_exit_prog(status, 0);
  76. }
  77.  
  78. void PyThread__exit_prog _P1(status, int status)
  79. {
  80.     do_PyThread_exit_prog(status, 1);
  81. }
  82. #endif /* NO_EXIT_PROG */
  83.  
  84. /*
  85.  * Lock support.
  86.  */
  87. PyThread_type_lock PyThread_allocate_lock _P0()
  88. {
  89.     mutex_t lock;
  90.  
  91.     dprintf(("PyThread_allocate_lock called\n"));
  92.     if (!initialized)
  93.         PyThread_init_thread();
  94.  
  95.     lock = mutex_alloc();
  96.     if (mutex_init(lock)) {
  97.         perror("mutex_init");
  98.         free((void *) lock);
  99.         lock = 0;
  100.     }
  101.     dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock));
  102.     return (PyThread_type_lock) lock;
  103. }
  104.  
  105. void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
  106. {
  107.     dprintf(("PyThread_free_lock(%lx) called\n", (long)lock));
  108.     mutex_free(lock);
  109. }
  110.  
  111. int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
  112. {
  113.     int success = FALSE;
  114.  
  115.     dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
  116.     if (waitflag) {     /* blocking */
  117.         mutex_lock(lock);
  118.         success = TRUE;
  119.     } else {        /* non blocking */
  120.         success = mutex_try_lock(lock);
  121.     }
  122.     dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
  123.     return success;
  124. }
  125.  
  126. void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
  127. {
  128.     dprintf(("PyThread_release_lock(%lx) called\n", (long)lock));
  129.     mutex_unlock((mutex_t )lock);
  130. }
  131.  
  132. /*
  133.  * Semaphore support.
  134.  *
  135.  * This implementation is ripped directly from the pthreads implementation.
  136.  * Which is to say that it is 100% non-functional at this time.
  137.  *
  138.  * Assuming the page is still up, documentation can be found at:
  139.  *
  140.  * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
  141.  *
  142.  * Looking at the man page, it seems that one could easily implement a
  143.  * semaphore using a condition.
  144.  *
  145.  */
  146. PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
  147. {
  148.     char *sema = 0;
  149.     dprintf(("PyThread_allocate_sema called\n"));
  150.     if (!initialized)
  151.         PyThread_init_thread();
  152.  
  153.     dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema));
  154.     return (PyThread_type_sema) sema;
  155. }
  156.  
  157. void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
  158. {
  159.     dprintf(("PyThread_free_sema(%lx) called\n", (long) sema));
  160. }
  161.  
  162. int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
  163. {
  164.     dprintf(("PyThread_down_sema(%lx, %d) called\n", (long) sema, waitflag));
  165.     dprintf(("PyThread_down_sema(%lx) return\n", (long) sema));
  166.     return -1;
  167. }
  168.  
  169. void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
  170. {
  171.     dprintf(("PyThread_up_sema(%lx)\n", (long) sema));
  172. }
  173.